package org.bigjava.biz.impl;

import org.bigjava.bean.Wuye;
import org.bigjava.biz.WuyeService;
import org.bigjava.common.Cryptograghy;
import org.bigjava.mapper.WuyeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.security.NoSuchAlgorithmException;
import java.util.List;

@Service("wuyeService")
public class WuyeServiceImpl implements WuyeService {

    @Qualifier("wuyeMapper")
    @Autowired
    WuyeMapper wuyeMapper;
    Cryptograghy cryptograghy = new Cryptograghy();

    //根据负责人姓名和物业公司模糊查询
    public List<Wuye> wuYeMoHu(String realname,String company){ return wuyeMapper.wuYeMoHu(realname,company); }
    //校验物业账号是否存在
    public Wuye wuYeJiaoYan(String username){
        return wuyeMapper.wuYeJiaoYan(username);
    }
    //根据id查出物业信息
    public Wuye wuYeId(int id){
        return wuyeMapper.wuYeId(id);
    }
    //根据id假删除物业
    public int wuYeShanChu(int id){
        return wuyeMapper.wuYeShanChu(id);
    }
    //物业总人数
    public int wuYeZong(){
        return wuyeMapper.wuYeZong();
    }
    //物业分页查询
    public List<Wuye> wuYeFenYe(int offset , int length){
        return wuyeMapper.wuYeFenYe(offset , length);
    }
    //物业账号密码登录
    public Wuye wuyeDengLu(String username,String password){
        try {
            password = cryptograghy.sha1(password);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return wuyeMapper.wuyeDengLu(username,password);
    }

    //物业个人信息修改
    public int wuYeXiuGai(Wuye wuye){
        Wuye wuyea = wuYeId(wuye.getId());
        if(wuye.getPassword()==""){
            wuye.setPassword(wuyea.getPassword());
        }else{
            //sha1加密
            try {
                wuye.setPassword(cryptograghy.sha1(wuye.getPassword()));
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            }
        }
        return wuyeMapper.wuYeXiuGai(wuye);
    }
    //添加物业
    public int wuYeAdd(Wuye wuye){
        try {
            wuye.setPassword(cryptograghy.sha1(wuye.getPassword()));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return wuyeMapper.wuYeAdd(wuye);
    }
    //查询全部物业
    public List<Wuye> wuYeQuan(){ return wuyeMapper.wuYeQuan(); }
}
